package com.hd.crdao.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.base.BaseActivity;
import com.hd.crdao.bean.MyDomainBean;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 *
 *@author Administrator
 *@date 2017/7/4 0004
 */

public class MyDomainActivity extends BaseActivity {
    @BindView(R.id.user_agreement_head)
    ExpandHeadView mUserAgreementHead;
    @BindView(R.id.domain_status)
    TextView mDomainStatus;
    @BindView(R.id.domaim)
    TextView mDomaim;
    @BindView(R.id.price)
    TextView mPrice;
    @BindView(R.id.cancle)
    TextView mCancle;
    @BindView(R.id.pay)
    TextView mPay;
    public String mBusinessid;
    public String mDomainId;
    public String mDomainName;
    public double mDomainPrice;
    @BindView(R.id.desc)
    TextView mDesc;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mydomain;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initListener() {
        mUserAgreementHead.getLeftArrowLL().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        mBusinessid = SPUtils.getString("businessid", "");
        loadData(mBusinessid);
    }


    @OnClick({R.id.cancle, R.id.pay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cancle:   //取消订单
                cancleOrder(mBusinessid, mDomainId);
                break;
            case R.id.pay:  //付款
                Intent intent = new Intent(UIUtils.getContext(), OrderCheckActivity.class);
                intent.putExtra("domainName", mDomainName);
                intent.putExtra("domainPrice", mDomainPrice);
                intent.putExtra("plOrder", SPUtils.getString("plOrder", ""));
                startActivity(intent);
                break;
        }
    }

    /**
     * 调接口取消未支付的订单
     */
    private void cancleOrder(String businessid, String domainId) {

        try {
            OkHttpUtils.post().url(Constants.CANCLEORDER_URL).addParams("domainId", domainId)
                    .addParams("carBusinessId", businessid).addParams("sign", Constants.SIGN).build().execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    MyToast.show("数据加载失败,请检查网络");
                }

                @Override
                public void onResponse(String response, int id) {
                    JSONObject jsonObject;
                    try {
                        jsonObject = new JSONObject(response);
                        String success = (String) jsonObject.get("success");
                        String message = (String) jsonObject.get("message");
                        ALog.d("success=" + success);
                        ALog.d("message=" + message);
                        if (TextUtils.equals("00", success)) {
                            startActivity(new Intent(UIUtils.getContext(), MyShopActivity.class));
                            finish();
                        } else {
                            MyToast.show(message);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 从服务器获取数据加载
     */
    private void loadData(String businessid) {

        try {
            OkHttpUtils.post().url(Constants.DOMAIN_NAME_LIST_URL).addParams("pageNo", "1").addParams("pageSize", "30")
                    .addParams("carBusinessId", businessid).addParams("sign", Constants.SIGN).build().execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    MyToast.show("数据加载失败,请检查网络");
                }

                @Override
                public void onResponse(String response, int id) {
                    ALog.d(response);
                    Gson gson = new Gson();
                    MyDomainBean myDomainBean = gson.fromJson(response, MyDomainBean.class);
                    MyDomainBean.DomainNamesBean domainNames = myDomainBean.getDomainNames();
                    String domainState = domainNames.getDomainState();
                    mDomainName = domainNames.getDomainName();
                    mDomainPrice = domainNames.getDomainPrice();
                    mDomainId = domainNames.getId();
                    if (TextUtils.equals("0", domainState)) {
                        mDomainStatus.setText("立即购买");
                        mPrice.setText("￥" + domainNames.getDomainPrice());
                    } else if (TextUtils.equals("3", domainState)) {
                        mDomainStatus.setText("待付款");
                        mDomainStatus.setVisibility(View.VISIBLE);
                        mCancle.setVisibility(View.VISIBLE);
                        mPay.setVisibility(View.VISIBLE);
                        mDesc.setVisibility(View.VISIBLE);
                        mPrice.setText("￥" + domainNames.getDomainPrice());
                    } else if (TextUtils.equals("5", domainState)) {
//                        mDomainStatus.setText("已被注册");  //已经购买显示的视图不一样   需要隐藏某些控件
                        mPrice.setText("订购时间: "+domainNames.getBuyDate());
                    } else if (TextUtils.equals("7", domainState)) {
                        mDomainStatus.setText("已过期");
                    }
                    mDomaim.setText(domainNames.getDomainName());
                }

            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
